from django.contrib import admin
from MyBlog.my_admin_site import my_site
from app.admin import MyBaseAdmin
from learningPlanet.adminforms import BlogAdminForm
from learningPlanet.models import JudgeTable, CollectTable, Tag, Blog, Category, SearchTable, Recommend


@admin.register(JudgeTable, site=my_site)
class JudgeTableAdmin(admin.ModelAdmin, MyBaseAdmin):
    list_display = ['id', 'judger', 'judgeBlog', 'content', 'judgeTime', 'isShow']
    list_display_links = list_display[:1]  # 默认的链接是id
    search_fields = list_display  # 搜索的字段同list_display
    list_filter = list_display[1:3]  # 过滤字段为前3个字段


@admin.register(CollectTable, site=my_site)
class CollectTableAdmin(admin.ModelAdmin):
    list_display = ['id', 'collectorId', 'blogId', 'isCollected', 'collectTime']
    list_display_links = list_display[:1]  # 默认的链接是id
    search_fields = list_display  # 搜索的字段同list_display
    list_filter = list_display[1:3]  # 过滤字段为前3个字段


@admin.register(Category, site=my_site)
class CategoryAdmin(admin.ModelAdmin, MyBaseAdmin):
    list_display = ['id', 'title', 'createdTime', 'used_count']
    list_display_links = list_display[:1]  # 默认的链接是id
    search_fields = list_display  # 搜索的字段同list_display
    list_filter = list_display[1:3]  # 过滤字段为前3个字段

    def used_count(self, obj):
        # 统计每个分类下的文章数量
        count = Blog.get_count_by_category_id(obj.id)
        return f'{count}'

    used_count.short_description = '该分类下的文章数量'


# 标签
@admin.register(Tag, site=my_site)
class TagAdmin(admin.ModelAdmin, MyBaseAdmin):
    list_display = ['id', 'title', 'create_time', 'creater']
    list_display_links = list_display[:1]  # 默认的链接是id
    search_fields = list_display  # 搜索的字段同list_display
    list_filter = list_display[1:3]  # 过滤字段为前3个字段


# 文章
@admin.register(Blog, site=my_site)
class BlogAdmin(admin.ModelAdmin, MyBaseAdmin):
    form = BlogAdminForm
    list_display = ['id', 'author', 'title', 'category', 'total_views', 'total_likes', 'total_collect', 'createdTime',
                    'updatedTime', 'isShow', 'isTop']
    list_display_links = list_display[:1]  # 默认的链接是id
    search_fields = list_display  # 搜索的字段同list_display
    list_filter = list_display[1:3]  # 过滤字段为前3个字段


# 搜索记录表
@admin.register(SearchTable, site=my_site)
class SearchTableAdmin(admin.ModelAdmin, MyBaseAdmin):
    list_display = ['id', 'keyword', 'createdTime']
    list_display_links = list_display[:1]  # 默认的链接是id
    search_fields = list_display  # 搜索的字段同list_display
    list_filter = list_display[1:3]  # 过滤字段为前3个字段


# 推荐
@admin.register(Recommend, site=my_site)
class RecommendAdmin(admin.ModelAdmin, MyBaseAdmin):
    list_display = ['id', 'tilte', 'weight']
    list_display_links = list_display[:1]  # 默认的链接是id
    search_fields = list_display  # 搜索的字段同list_display
    list_filter = list_display[1:3]  # 过滤字段为前3个字段
